{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 部署验证码识别服务\n",
    "\n",
    "## 另存为 app.py，启动 flask 来加载 app.py 文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "\n",
    "from io import BytesIO\n",
    "from flask import Flask, request, jsonify\n",
    "from keras.models import load_model\n",
    "from PIL import Image\n",
    "\n",
    "NUMBER = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n",
    "LOWERCASE = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',\n",
    "            'v', 'w', 'x', 'y', 'z']\n",
    "UPPERCASE = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',\n",
    "           'V', 'W', 'X', 'Y', 'Z']\n",
    "\n",
    "CAPTCHA_CHARSET = NUMBER   # 验证码字符集\n",
    "CAPTCHA_LEN = 4            # 验证码长度\n",
    "CAPTCHA_HEIGHT = 60        # 验证码高度\n",
    "CAPTCHA_WIDTH = 160        # 验证码宽度\n",
    "\n",
    "# 10 个 Epochs 训练的模型\n",
    "MODEL_FILE = './pre-trained/model/captcha_rmsprop_binary_crossentropy_bs_100_epochs_10.h5'\n",
    "\n",
    "def vec2text(vector):\n",
    "    if not isinstance(vector, np.ndarray):\n",
    "        vector = np.asarray(vector)\n",
    "    vector = np.reshape(vector, [CAPTCHA_LEN, -1])\n",
    "    text = ''\n",
    "    for item in vector:\n",
    "        text += CAPTCHA_CHARSET[np.argmax(item)]\n",
    "    return text\n",
    "\n",
    "def rgb2gray(img):\n",
    "    # Y' = 0.299 R + 0.587 G + 0.114 B \n",
    "    # https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale\n",
    "    return np.dot(img[...,:3], [0.299, 0.587, 0.114])\n",
    "\n",
    "app = Flask(__name__) # 创建 Flask 实例\n",
    "\n",
    "# 测试 URL\n",
    "@app.route('/ping', methods=['GET', 'POST'])\n",
    "def hello_world():\n",
    "    return 'pong'\n",
    "\n",
    "# 验证码识别 URL\n",
    "@app.route('/predict', methods=['POST'])\n",
    "def predict():\n",
    "    response = {'success': False, 'prediction': '', 'debug': 'error'}\n",
    "    received_image= False\n",
    "    if request.method == 'POST':\n",
    "        if request.files.get('image'): # 图像文件\n",
    "            image = request.files['image'].read()\n",
    "            received_image = True\n",
    "            response['debug'] = 'get image'\n",
    "        elif request.get_json(): # base64 编码的图像文件\n",
    "            encoded_image = request.get_json()['image']\n",
    "            image = base64.b64decode(encoded_image)\n",
    "            received_image = True\n",
    "            response['debug'] = 'get json'\n",
    "        if received_image:\n",
    "            image = np.array(Image.open(BytesIO(image)))\n",
    "            image = rgb2gray(image).reshape(1, 60, 160, 1).astype('float32') / 255\n",
    "            with graph.as_default():\n",
    "                pred = model.predict(image)\n",
    "            response['prediction'] = response['prediction'] + vec2text(pred)\n",
    "            response['success'] = True\n",
    "            response['debug'] = 'predicted'\n",
    "    else:\n",
    "        response['debug'] = 'No Post'\n",
    "    return jsonify(response)\n",
    "\n",
    "model = load_model(MODEL_FILE) # 加载模型\n",
    "graph = tf.get_default_graph() # 获取 TensorFlow 默认数据流图"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
